#include <head.h>

int main(int argc, char const *argv[])
{
    pid_t pid = fork();
    int wait_read = 0;
    if (-1 == pid)
    {
        perror("fork");
        return 1;
    }

    if (pid == 0) // 子进程
    {
        printf("子进程 = %d\n", getpid());
        sleep(100);
        printf("子进程结束\n");
        exit(5);
    }
    else if (pid > 0) // 父进程
    {
        printf("父进程 = %d\n", getpid());
        sleep(3);
        kill(pid, SIGSTOP); // 暂停子进程
        sleep(3);
        kill(pid, SIGCONT); // 继续子进程
    }

    waitpid(pid, &wait_read, WNOHANG);
    if (WIFEXITED(wait_read))
    {
        printf("子进程正常退出,退出码 = %d\n", WEXITSTATUS(wait_read));
    }
    else if (WIFSIGNALED(wait_read))
    {
        printf("子进程异常退出,信号 = %d\n", WTERMSIG(wait_read));
    }
    waitpid(pid, &wait_read, WUNTRACED);
    if (WIFSTOPPED(wait_read))
    {
        printf("子进程暂停,信号 = %d\n", WSTOPSIG(wait_read));
    }
    printf("子进程暂停成功\n");
    
    waitpid(pid, &wait_read, WCONTINUED);
    if (WIFCONTINUED(wait_read))
    {
        printf("子进程继续\n");
    }
    printf("子进程恢复成功\n");

    while (1)
    {
        sleep(1);
    }

    return 0;
}
